<?php

function insertProb($conn,$dataInicio,$dataFim){
$titulo = trim($_POST["probTitulo"]);
$prob_text = trim($_POST["prob_text"]);
$resposta = trim($_POST["resposta"]);
$sugestoes = trim($_POST["sugestoes"]);
$id = $_SESSION["id"];

$titulo = $conn->real_escape_string($titulo);
$prob_text = $conn->real_escape_string($prob_text);
$resposta = $conn->real_escape_string($resposta);
$sugestoes = $conn->real_escape_string($sugestoes);

$query = "INSERT INTO problemas_tbl (id_autor, titulo, problema, resposta, sugestoes, publicado, 
			data_criacao, data_mod)
			VALUES ($id,'". iconv("utf-8", "windows-1252", $titulo)."',
			'".iconv("utf-8", "windows-1252", $prob_text)."',
			'".iconv("utf-8", "windows-1252", $resposta)."',
			'".iconv("utf-8", "windows-1252", $sugestoes)."',1,
			NOW(), null)";

$result = $conn->query($query);
echo $conn->error;

$lastID = $conn->insert_id;

$query = "SELECT `id` FROM `epocas_tbl`
WHERE `data_ini` < '".$dataInicio."' AND `data_fim` > '".$dataFim."'";

$result = $conn->query($query);
$linha = $result->fetch_object();
$epocaID = $linha->id;


$query = "INSERT INTO `jornadas_tbl`(`id_autor`, `id_epoca`,
	`id_problema`, `data_ini`, `data_fim`, `data_cria`, `data_mod`)
	VALUES ($id,$epocaID,$lastID,'".$dataInicio."','".$dataFim."',NOW(),null)";

$result = $conn->query($query);
echo $conn->error;

//var_dump($_POST);

if (!$result)
	header("location: ../publico/insertion_fail.php");
header("location: ../publico/insertion_success.php");
}

function updateProb($id, $conn){
$titulo = trim($_POST["probTitulo"]);
$prob_text = trim($_POST["prob_text"]);
$resposta = trim($_POST["resposta"]);
$sugestoes = trim($_POST["sugestoes"]);
$id = $_GET["id"];

$titulo = $conn->real_escape_string($titulo);
$prob_text = $conn->real_escape_string($prob_text);
$resposta = $conn->real_escape_string($resposta);
$sugestoes = $conn->real_escape_string($sugestoes);

$query = "UPDATE problemas_tbl SET titulo='". iconv("utf-8", "windows-1252", $titulo)."',
			problema='".iconv("utf-8", "windows-1252", $prob_text)."',
			resposta='".iconv("utf-8", "windows-1252", $resposta)."',
			sugestoes='".iconv("utf-8", "windows-1252", $sugestoes)."',
			data_mod=NOW()
			WHERE id=".$id;

$result = $conn->query($query);
echo $conn->error;

}

function getProb($id, $conn){
	
	$query = "SELECT id,titulo, problema, resposta, sugestoes
	FROM problemas_tbl
	WHERE id=".$id."";
	
	$result = $conn->query($query);
	
	$prob = array();
	$linha = $result->fetch_object();
	$prob["id"] = $linha->id;
	$prob["titulo"] = iconv("windows-1252", "utf-8", $linha->titulo);
	$prob["problema"] = iconv("windows-1252", "utf-8", $linha->problema);
	$prob["resposta"] = iconv("windows-1252", "utf-8", $linha->resposta);
	$prob["sugestoes"] = iconv("windows-1252", "utf-8", $linha->sugestoes);

	return $prob;
}

function getProbSemana($conn){
	
	$query = "SELECT id_problema
	FROM jornadas_tbl
	WHERE `data_ini` <= NOW() AND `data_fim` >= NOW()";
	
	$result = $conn->query($query);
	echo $conn->error;
	
	if($result->num_rows != 0){
		$prob = array();
		$linha = $result->fetch_object();
		$prob["id"] = $linha->id_problema;
		
		$query = "SELECT titulo, problema
		FROM problemas_tbl
		WHERE id=".$prob["id"];
		
		$result = $conn->query($query);
		$linha = $result->fetch_object();
		$prob["titulo"] = $linha->titulo;
		$prob["problema"] = $linha->problema;
		
		return $prob;
	}
}

?>